package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CustomerDao;
import cn.gjculture.shopping.api.dao.CustomerMapper;
import cn.gjculture.shopping.api.dao.JiangRecordDao;
import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.export.CustomerExportExcel;
import cn.gjculture.shopping.api.export.ExportExcel;
import cn.gjculture.shopping.api.service.CustomerService;
import cn.gjculture.shopping.api.service.PayService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.SpringContent;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    @Autowired
    PayService payService;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    JiangRecordDao jiangRecordDao;

    @Override
    public int insert(Customer customer, Return<Customer> ret) {
        customer.setId(UUIDUtil.getUUID());
        customer.setInsertTime(DateUtil.getTimestamp());
        customer.setUpdateTime(DateUtil.getTimestamp());
        customer.setNickname("国匠商城微信用户");
        if(customerDao.insert(customer)>0){
            ret.setData(customer);
            return 1;
        }
        return 0;

    }

    @Override
    public Customer queryByOpenid(String openid) {
        return customerDao.queryByOpenid(openid);
    }

    @Override
    public int setPhoneVerify(Customer customer) {
        return customerDao.setPhoneVerify(customer);
    }

    @Override
    public List<Customer> query(Customer customer) {
        List<Customer> customers=customerMapper.query(customer);
        return customers;
    }

    @Override
    public List<Customer> queryVip() {
        return customerDao.queryVip();
    }

    @Override
    public List<Customer> queryRegisterCustomer() {
        List<Customer> customers=customerDao.queryRegisterCustomer();
        if(!CollectionUtils.isEmpty(customers)){
            for(Customer c:customers){
                collect(c);
            }
        }
        return customers;
    }
    @Override
    public Wallet queryWallet(String customerId) {
        Wallet wallet=new Wallet();
        wallet.setId(customerId);
        BigDecimal canWithdraw=new BigDecimal("0.00");
        BigDecimal balance=new BigDecimal("0.00");
        BigDecimal toAccount=new BigDecimal("0.00");
        List<SubAccount> subAccounts=payService.querySubAccount(customerId);
        if(subAccounts!=null&&subAccounts.size()>0){
            for(SubAccount subAccount:subAccounts){
                if(subAccount.getDeadline()<=System.currentTimeMillis()){
                    canWithdraw=canWithdraw.add(new BigDecimal(subAccount.getAmount()));
                }else{
                    toAccount=toAccount.add(new BigDecimal(subAccount.getAmount()));
                }
                balance=balance.add(new BigDecimal(subAccount.getAmount()));
            }
            wallet.setBalance(balance.setScale(2,BigDecimal.ROUND_DOWN).toString());
            wallet.setCanWithdraw(canWithdraw.setScale(2,BigDecimal.ROUND_DOWN).toString());
            wallet.setToAccount(toAccount.setScale(2,BigDecimal.ROUND_DOWN).toString());
        }
        return wallet;
    }

    @Override
    public Customer queryById(String customerId) {
        return customerDao.queryById(customerId);
    }

    @Override
    public int upLevel(UpLevelParams upLevelParams) {
        upLevelParams.setUpdateTime(System.currentTimeMillis());
        if(upLevelParams.getJiang()!=null&&upLevelParams.getJiang()>0&&!StringUtils.isEmpty(upLevelParams.getPhone())){
            saveUseScore(upLevelParams.getJiang(),upLevelParams.getIsPlus(),upLevelParams.getPhone());
        }
        return customerMapper.upLevel(upLevelParams);
    }

    @Override
    public void export(Customer customer, HttpServletResponse response) {
        List<Customer> customers=this.query(customer);
        if(CollectionUtils.isEmpty(customers)) return;
        ExportExcel exportExcel= SpringContent.getBean(CustomerExportExcel.class);
        exportExcel.setDatas(customers);
        exportExcel.setSheetName("用户列表");
        try {
            exportExcel.export(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void collect(Customer c) {
        if(c!=null){
            c.setWallet(this.queryWallet(c.getId()));
        }
    }



    private boolean saveUseScore(Integer jiang, Integer isPlus,String phone) {
        Customer customer = customerDao.queryByPhone(phone);
        JiangRecord scoreRecord = new JiangRecord();
        scoreRecord.setScoreRecordId(UUIDUtil.getUUID());
        int surplus;
        if(isPlus==2){
            surplus=customer.getJiang()-jiang;
            scoreRecord.setScoreUse("-" + jiang);
        }else{
            surplus=customer.getJiang()+jiang;
            scoreRecord.setScoreUse("+" + jiang);
        }
        scoreRecord.setCustomerId(customer.getId());
        scoreRecord.setDesc("系统操作");
        scoreRecord.setUseTime(DateUtil.getTimestamp());
        scoreRecord.setScoreSurPlus(surplus+"");
        jiangRecordDao.insert(scoreRecord);
        return true;
    }
}
